package com.dhcc.core.config.properties;

import cn.hutool.core.net.NetUtil;
import com.dhcc.core.framework.constant.Constant;
import com.dhcc.core.framework.constant.SsoClientTypeConst;
import com.dhcc.core.framework.util.ArrayUtil;
import com.dhcc.core.framework.util.HttpContextUtil;
import com.dhcc.core.framework.util.SpringContextHolder;
import org.pac4j.core.context.ContextHelper;
import org.pac4j.core.context.HttpConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * SSO 单点配置
 *
 * @ClassName: SsoProperties
 * @Description: 描述
 * @author: cyf
 * @date: 2018年1月2日 上午9:41:42
 */
@Component
@Order(value=2)
@ConfigurationProperties(prefix = SsoProperties.PREFIX)
@DependsOn("springContextHolder")
public class SsoProperties {

    public static final String PREFIX = "sso";

    public static SsoProperties me() {
        return SpringContextHolder.getBean(SsoProperties.class);
    }

    private String[] types;

    private String serverUrl;

    private String serverUrlForNet;
    
    private String hosBackendUrl;
    
    private String hosFrontUrl;

    private String oauthKey;

    private String oauthSecret;


    public String getServerUrlForNet() {
        return serverUrlForNet;
    }

    public void setServerUrlForNet(String serverUrlForNet) {
        this.serverUrlForNet = serverUrlForNet;
    }

    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getOauthKey() {
        return oauthKey;
    }

    public void setOauthKey(String oauthKey) {
        this.oauthKey = oauthKey;
    }

    public String getOauthSecret() {
        return oauthSecret;
    }

    public void setOauthSecret(String oauthSecret) {
        this.oauthSecret = oauthSecret;
    }

	public static String getPrefix() {
		return PREFIX;
	}

    public String[] getTypes() {
        return types;
    }

    public void setTypes(String[] types) {
        this.types = types;
    }

    public String getDefaultType() {
        return types[0];
    }

    public Boolean hasForm() {
        return ArrayUtil.contains(types, SsoClientTypeConst.FORM);
    }

    public String getTypesStr() {
        return ArrayUtil.join(types, Constant.SEPARATOR_COMMA);
    }
    /**
     * 动态取服务器地址
     */
    public String getDynamicServerUrl() {
        String serverName = HttpContextUtil.getRequest().getServerName();
        if (NetUtil.isInnerIP(serverName)) {
            return this.getServerUrl();
        }
        return this.getServerUrlForNet();
    }
    
    public String getHosBackendUrl() {
		return hosBackendUrl;
	}

	public void setHosBackendUrl(String hosBackendUrl) {
		this.hosBackendUrl = hosBackendUrl;
	}

	public String getHosFrontUrl() {
		return hosFrontUrl;
	}

	public void setHosFrontUrl(String hosFrontUrl) {
		this.hosFrontUrl = hosFrontUrl;
	}

	/**
     * 动态取客户端地址
     */
    public String getDynamicClientUrl() {

        HttpServletRequest request = HttpContextUtil.getRequest();

        String serverName = request.getServerName();
        String scheme = request.getScheme();
        int port = request.getServerPort();

        final StringBuilder sb = new StringBuilder();

        sb.append(scheme).append("://").append(serverName);

        final boolean notDefaultHttpPort = ContextHelper.SCHEME_HTTP.equalsIgnoreCase(scheme)
                && port != HttpConstants.DEFAULT_HTTP_PORT;
        final boolean notDefaultHttpsPort = ContextHelper.SCHEME_HTTPS.equalsIgnoreCase(scheme)
                && port != HttpConstants.DEFAULT_HTTPS_PORT;
        if (notDefaultHttpPort || notDefaultHttpsPort) {
            sb.append(":").append(port);
        }

        return sb.toString();
    }
}
